'''
Created on Jan 30, 2010

@author: oabalbin
'''
import sys
import MySQLdb;
import numpy as np
from collections import deque, defaultdict
from signatures.common.classes import record, genrecord, arrayMatrix, cpmMatrix, drugBankrecord
#from signatures.preprocess.tools import filter_constant_genes
# To remove once you set up pipeline file
#from signatures.preprocess.tools import get_variable_genes
#from signatures.seedgenes.seedlist import get_seed_genes, write_varin_file

class query():
    
    def __init__(self, dbhost, dbuser, dbpasswd, database):
        """ Database Information """
        self.myhost = dbhost
        self.myuser = dbuser
        self.mypasswd = dbpasswd
        self.mydb = database
    
    def create_connector(self):
        self.conn=MySQLdb.connect(host=self.myhost,user=self.myuser,passwd=self.mypasswd,db=self.mydb)
    
    def close_connector(self, connector):
        connector.close

    
    def get_gen_exp(self,sample):
        
        cursor=self.conn.cursor()
        query = 'select probeID, geneSym, sample, log_fold from erg_signatures where sample=\"' + sample +'\";'
        
        cursor.execute(query)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        
        genes = []
        for row in results:
            genes.append( record(row[0],row[1],row[2],float(row[3])) )
            
            
        return genes
    
    
    def get_gensample_exp(self, samplelist, genlist, gensampleDic,tableName):
        """
        It returns genes which are in all samples. 
        Report the median value for the expression for the probes of that gene and the std
        for the expression of the set of probes.
        """
        
        #ind=0
        notshared=[]
        for j, gen in enumerate(genlist):            
            #print str(j)
            cursor=self.conn.cursor()
            query = 'select probeID, geneSym, sample, log_fold from '+tableName+' where geneSym=\"' + gen +'\";' #erg_signatures
            cursor.execute(query)
            # Fetch all the rows in a list of lists.
            results = cursor.fetchall()
    
            probes,expval={},{}
            for row in results:   
                try:
                    probes[row[2]].append(row[0])
                    expval[row[2]].append(float(row[3]))
                except KeyError:
                    probes[row[2]] = [row[0]]
                    expval[row[2]] = [float(row[3])]
                    
            #print j, gen, len(set(samplelist)), len(set(probes.keys())), len(set(samplelist).intersection(set(probes.keys()))) 
            
            if len(set(samplelist).intersection(set(probes.keys()))) == len(samplelist):
            
            #print j, gen, len(set(samplelist)), len(set(probes.keys())), len(set(samplelist).intersection(set(probes.keys())))
            #if set(samplelist) == set(probes.keys()):    
                #for sample, ep in expval.iteritems():
                 
                for sample in samplelist:
                    #print sample, ep 
                    ep = expval[sample]
                    med_exp = np.median(ep)
                    std_exp = np.std(ep)
                           
                    gensampleDic[sample].append( genrecord(probes,gen,sample,med_exp,std_exp) )
            else:
                notshared.append(gen)    
                    #print gen, sample, gensampleDic['PREC_AD_ERG1_LACZ_dup'][-1].sample, str(len(gensampleDic['PREC_AD_ERG1_LACZ_dup']))
                    #if j > 3:
                    #if len(gensampleDic['PREC_AD_ERG1_LACZ_dup']) > len(genlist):
                    #    print gen, sample
                    #    sys.exit(0)
            #print j, gen, len(results), len(expval.keys())
            #print '\t'.join(['\t'.join((k,str(len(v)))) for k,v in gensampleDic.items()])
            #gensampleDic.keys()
            #count += 1
            #if count > 10:
            #    sys.exit(0)
        
        print 'The number of genes not shared between studies = ' + str(len(notshared))
        #print notshared
        
        return gensampleDic
    
    
    def get_gensample_exp_pval(self, samplelist, genlist, gensampleDic,tableName, pval):
        """
        It returns genes which are in all samples. 
        Report the median value for the expression for the probes of that gene and the std
        for the expression of the set of probes.
        """
        
        #ind=0
        notshared=[]
        for j, gen in enumerate(genlist):            
            #print str(j)
            cursor=self.conn.cursor()
            query = 'select probeID, geneSym, sample, log_fold from '+tableName+' where geneSym=\"' + gen +'\" and pvalue <= \"' + pval +'\" ;' #erg_signatures
            cursor.execute(query)
            # Fetch all the rows in a list of lists.
            results = cursor.fetchall()
            
            probes,expval={},{}
            for row in results:   
                try:
                    probes[row[2]].append(row[0])
                    expval[row[2]].append(float(row[3]))
                except KeyError:
                    probes[row[2]] = [row[0]]
                    expval[row[2]] = [float(row[3])]
                    
            #print j, gen, len(set(samplelist)), len(set(probes.keys())), len(set(samplelist).intersection(set(probes.keys()))) 
            
            if len(set(samplelist).intersection(set(probes.keys()))) == len(samplelist):
            
            #print j, gen, len(set(samplelist)), len(set(probes.keys())), len(set(samplelist).intersection(set(probes.keys())))
            #if set(samplelist) == set(probes.keys()):    
                #for sample, ep in expval.iteritems():
                 
                for sample in samplelist:
                    #print sample, ep 
                    ep = expval[sample]
                    med_exp = np.mean(ep)
                    std_exp = np.std(ep)
                    
                    gensampleDic[sample].append( genrecord(probes,gen,sample,med_exp,std_exp) )
                        
            else:
                notshared.append(gen)    
        
        print 'The number of genes not shared between studies = ' + str(len(notshared))
        #print notshared
        
        return gensampleDic

    
    
    def get_gensample_plain(self, samplelist, genlist, gensampleDic,tableName):
        """
        It returns genes which are in all samples. 
        Report the median value for the expression for the probes of that gene and the std
        for the expression of the set of probes.
        """
        #ind=0
        notshared=[]
        for j, gen in enumerate(genlist):
            #print j, gen
            #print str(j)
            cursor=self.conn.cursor()
            query = 'select probeID, geneSym, sample, log_fold from '+tableName+' where geneSym=\"' + gen +'\";' #erg_signatures
            cursor.execute(query)
            # Fetch all the rows in a list of lists.
            results = cursor.fetchall()
            
            probes,expval={},{}
            for row in results:   
                try:
                    probes[row[2]].append(row[0])
                    expval[row[2]].append(float(row[3]))
                except KeyError:
                    probes[row[2]] = [row[0]]
                    expval[row[2]] = [float(row[3])]
            
            if set(samplelist) == set(probes.keys()):
                
                for sample, ep in expval.iteritems(): 
                    l= len(ep)
                    med_exp = np.median(ep)
                    std_exp = np.std(ep)
                    
                    for k in range(l):
                        if ep[k]=='':
                            genexp=med_exp
                        else:
                            genexp=ep[k]
                            
                        gensampleDic[sample].append( genrecord(probes[sample][k],gen,sample,genexp,0) )
            else:
                notshared.append(gen)    
        
        print 'The number of genes not shared between studies = ' + str(len(notshared))
        print notshared
        
        return gensampleDic

    
    
    
    def get_exp_mat(self,samplelist):
        """
        returns an array class that contains genelist, samplelist, and 
        a numpy array with the expression of the genes in each sample in samplelist 
        """
        genelist=[]
        
        expmat = arrayMatrix(0, 0)
        for j, sample in enumerate(samplelist):
            print sample
            genelist = self.get_gen_exp(sample)
            if j == 0:
                ngenes = len(genelist)
                nsamples = len(samplelist)
                expmat = arrayMatrix(ngenes, nsamples)
            
            # add instead of append. Change of list for set
            expmat.sample.add(sample)
            for i, g in enumerate(genelist):
                expmat.probeID.add(g.probeID)
                expmat.geneSym.add(g.geneSym)    
                expmat.expVal[i,j] = g.expVal
        
        # EYE:: Transform log fold change to log2
        expmat.expVal = np.log2(expmat.expVal)

        return expmat
    
    def get_exp_mat_2(self,samplelist, genelist, tableName):
        """
        returns an array class that contains genelist, samplelist, and 
        a numpy array with the expression of the genes in each sample in samplelist 
        """
        genesampleDic={}
        expmat = arrayMatrix(0, 0)
        
        for sp in samplelist:
            genesampleDic[sp] = deque([])
            # NO: genesampleDic[sp] = genes. Reason: You are pointing everything to the same list
            
        # make a dictionary with the list of genes and samples
        genesampleDic = self.get_gensample_exp(samplelist,genelist, genesampleDic, tableName)
        #genesampleDic = self.get_gensample_plain(samplelist,genelist, genesampleDic, tableName)
        
        j=0
        #for sp, spgene in genesampleDic.iteritems():
        for sp in samplelist:
            spgene = genesampleDic[sp]
            
            print sp, len(spgene)
            if j == 0:
                ngenes = len(spgene)
                nsamples = len(samplelist)
                expmat = arrayMatrix(ngenes, nsamples)
                
            expmat.sample.append((j,sp))

            for i, g in enumerate(spgene):
                #print i, g.geneSym
                #if i == 0:
                    # See that you are potetially loosing information about the probes 
                    # by only using the record of the first sample 
                expmat.probeID.append(g.probeID)
                expmat.geneSym.append(g.geneSym)
                expmat.geneSynd.append((i,g.geneSym)) #.add((i,g.geneSym))
                expmat.expValu[i,j] = g.expVal
                expmat.stdVal[i,j] = g.std
                if g.expVal == 0:
                    expmat.expmask[i,j]=True
                else:
                    expmat.expmask[i,j]=False
                
                '''
                if g.geneSym=='ERG':
                    print sp, g.geneSym, g.expVal, g.std
                '''
            
            j+=1
        
        #print 'Number of symbols '+ str(len(expmat.geneSynd))
        
        return expmat
    
    def get_exp_mat_2pval(self,samplelist, genelist, tableName, pval):
        """
        returns an array class that contains genelist, samplelist, and 
        a numpy array with the expression of the genes in each sample in samplelist 
        """
        genesampleDic={}
        expmat = arrayMatrix(0, 0)
        
        for sp in samplelist:
            genesampleDic[sp] = deque([])
            # NO: genesampleDic[sp] = genes. Reason: You are pointing everything to the same list
            
        # make a dictionary with the list of genes and samples
        genesampleDic = self.get_gensample_exp_pval(samplelist,genelist, genesampleDic, tableName, pval)
        #genesampleDic = self.get_gensample_plain(samplelist,genelist, genesampleDic, tableName)
        
        j=0
        #for sp, spgene in genesampleDic.iteritems():
        for sp in samplelist:
            spgene = genesampleDic[sp]
            
            print sp, len(spgene)
            if j == 0:
                ngenes = len(spgene)
                nsamples = len(samplelist)
                expmat = arrayMatrix(ngenes, nsamples)
                
            expmat.sample.append((j,sp))

            for i, g in enumerate(spgene):
                #print i, g.geneSym
                #if i == 0:
                    # See that you are potetially loosing information about the probes 
                    # by only using the record of the first sample 
                expmat.probeID.append(g.probeID)
                expmat.geneSym.append(g.geneSym)
                expmat.geneSynd.append((i,g.geneSym)) #.add((i,g.geneSym))
                expmat.expValu[i,j] = g.expVal
                expmat.stdVal[i,j] = g.std
                if g.expVal == 0:
                    expmat.expmask[i,j]=True
                else:
                    expmat.expmask[i,j]=False
                
                '''
                if g.geneSym=='ERG':
                    print sp, g.geneSym, g.expVal, g.std
                '''
            
            j+=1
        
        #print 'Number of symbols '+ str(len(expmat.geneSynd))
        
        return expmat



    
    def get_commongen2samples(self,samplelist, genelist, tableName):
        """
        It returns genes which are in all samples. 
        Report the median value for the expression for the probes of that gene and the std
        for the expression of the set of probes.
        """
        shared=deque([])
        notshared=deque([])
        for j, gen in enumerate(genelist):            
            cursor=self.conn.cursor()
            query = 'select probeID, geneSym, sample, log_fold from '+tableName+' where geneSym=\"' + gen +'\";' #erg_signatures
            cursor.execute(query)
            # Fetch all the rows in a list of lists.
            results = cursor.fetchall()
            
            probes,expval={},{}
            for row in results:   
                try:
                    probes[row[2]].append(row[0])
                    expval[row[2]].append(float(row[3]))
                except KeyError:
                    probes[row[2]] = [row[0]]
                    expval[row[2]] = [float(row[3])]
                                
            if len(set(samplelist).intersection(set(probes.keys()))) == len(samplelist):
                shared.append(gen)
            else:
                notshared.append(gen)
                
        print 'The number of genes not shared between studies = ' + str(len(notshared))
        print notshared
        
        return shared


    
    
    def write_variable_genes(self, expMat, vargenes, matfile, genefile):
        """
        write an expression dataset. The data set is made only with  genes
        of high variability vargenes.
        """
        
        genelist = np.array(expMat.geneSym)
        genelist = genelist[vargenes]
        
        vg_expMat = expMat.expVal[vargenes,:].filled(fill_value=0)
        #samples = [ i[1] for i in expMat.sample]
        #genefile.write(",".join(expMat.sample).replace(",", "\n")+'\n') 
        #genefile.write(",".join(samples).replace(",", "\n")+'\n')
        for i, g in enumerate(genelist):
            el = map( str,list(vg_expMat[i,:]) ) 
            genefile.write(g+'\n')
            matfile.write(",".join(list( el )).replace(",", "\t") + '\n')
    
    
    def write_variable_genes_nfact(self, expMat, vargenes, matfile, genefile, spfile, ind=False):
        """
        write an expression dataset. The data set is made only with  genes
        of high variability vargenes.
        ind=True prints a matrix file with the vargenes as the first n rows and then the rest.
        This is because BFRM requires such organization. Note: bfrm no working yet with this disposition
        ind=false, writes the matrix in the original order.
        """
        # Sort vargenes. It assumes that the genelist is also sorted
        vargenes.sort()
        allgenelist = np.array(expMat.geneSym)
        allind = set(expMat.genInd.values())
        if ind:
            genelist = allgenelist[vargenes]
            vg_expMat = expMat.expVal[vargenes,:].filled(fill_value=0)

            
            for i, g in enumerate(genelist):
                el = map( str,list(vg_expMat[i,:]) ) 
                genefile.write(g+'\n')
                matfile.write(",".join(list( el )).replace(",", "\t") + '\n')
            
            out_gene = set(allgenelist)
            out_gene.difference_update(genelist)
            allind.difference_update(set(vargenes))
            
            print vargenes
            print allind
            print list(allind)
            
            aind = np.array(list(allind))
            
            vg_expMat = expMat.expVal[aind,:].filled(fill_value=0)
            
            for i, g in enumerate(list(out_gene)):
                el = map( str,list(vg_expMat[i,:]) ) 
                genefile.write(g+'\n')
                matfile.write(",".join(list( el )).replace(",", "\t") + '\n')


        else:
            genelist = allgenelist[vargenes]
            vg_expMat = expMat.expVal[vargenes,:].filled(fill_value=0)
       
            for i, g in enumerate(genelist):
                el = map( str,list(vg_expMat[i,:]) )                
                genefile.write(g+'\n')
                matfile.write(",".join(list( el )).replace(",", "\t") + '\n')
        
        samples = [ i[1] for i in expMat.sample]
        spfile.write('\t'+",".join(samples).replace(",", "\t")+'\n')
        
    def write_variable_genes_nfact_nrespon(self, expMat, vargenes, matfile, genefile, spfile, spclass0, spclass1=[], spcltest=[]):
        """
        write an expression dataset. The data set is made only with  genes
        of high variability vargenes.
        ind=True prints a matrix file with the vargenes as the first n rows and then the rest.
        This is because BFRM requires such organization. Note: bfrm no working yet with this disposition
        ind=false, writes the matrix in the original order.
        """
        
        genelist = np.array(expMat.geneSym)
        genelist = genelist[vargenes]
        samples = [ i[1] for i in expMat.sample]
        vg_expMat = expMat.expVal[vargenes,:]
        
        
        odsp=[]
        classvec=[]
        matdim = vg_expMat.shape
        ncols=(len(spclass0)+len(spclass1)+len(spcltest))
        
        print 'The number of columns in new matrix ' + str(ncols)
        odexpMat=arrayMatrix(matdim[0], ncols)
        print odexpMat.expVal.shape 
        spnotfound=[]
        sps=[]
        
        k=0
        
        if spclass0:
            print 'first class'
            for i, sp in enumerate(samples):
                if sp in spclass0:
                    print i,k,sp
                    #spind = samples2.index(sp)
                    odsp.insert(i, sp)
                    odexpMat.expVal[:,k] = vg_expMat[:,i]
                    classvec.append(0)
                    k+=1
                    sps.append(sp)
                else:
                    spnotfound.append(sp)
            print k
            
        if spclass1:  
            print 'second class'
            for i, sp in enumerate(samples):
                if sp in spclass1:
                    print i,k,sp
                    #spind = samples2.index(sp)
                    odsp.insert(i, sp)
                    odexpMat.expVal[:,k] = vg_expMat[:,i]
                    classvec.append(1)
                    k+=1
                    sps.append(sp)
                else:
                    spnotfound.append(sp)
        
            print k 
        
        
        if spcltest:
            print 'Third class'
            for i, sp in enumerate(samples):
                
                if sp in spcltest:
                    print i,k,sp
                    #spind = samples2.index(sp)
                    odsp.insert(i, sp)
                    odexpMat.expVal[:,k] = vg_expMat[:,i]
                    classvec.append(2)
                    k+=1
                    sps.append(sp)
                else:
                    spnotfound.append(sp)
        
        print k
        
        #matfile.write('\t'+",".join(sps).replace(",", "\t")+'\n')
        matfile.write('\t'+",".join(map(str,classvec)).replace(",", "\t")+'\n')
        for i, g in enumerate(genelist):
            el = map( str,list(odexpMat.expVal[i,:].filled(fill_value=0)) ) 
            genefile.write(g+'\n')
            matfile.write(",".join(list( el )).replace(",", "\t") + '\n')


        spfile.write('\t'+",".join(samples).replace(",", "\t")+'\n')




    def write_variable_genes_sdv(self, expMat, vargenes, matfile, genefile, spclass0, spclass1=[], spcltest=[]):
        """
        write an expression dataset. The data set is made only with  genes
        of high variability vargenes.
        Matrix is formatted for using with the sdv program (West,2003)
        spclass0= control (0), spclass1=treatment (1), spcltest=test (2)
        """
        
        vargenes.sort()
        genelist = np.array(expMat.geneSym)
        genelist = genelist[vargenes]
        samples = [ i[1] for i in expMat.sample]
        vg_expMat = expMat.expVal[vargenes,:]
        
        
        odsp=[]
        classvec=[]
        matdim = vg_expMat.shape
        ncols=(len(spclass0)+len(spclass1)+len(spcltest))
        
        print 'The number of columns in new matrix ' + str(ncols)
        odexpMat=arrayMatrix(matdim[0], ncols)
        print odexpMat.expVal.shape 
        spnotfound=[]
        sps=[]
        
        k=0
        
        if spclass0:
            print 'first class'
            for i, sp in enumerate(samples):
                if sp in spclass0:
                    print i,k,sp
                    #spind = samples2.index(sp)
                    # OJO con este insert que puede producir un bug si i no es consecutivo
                    odsp.insert(i, sp)
                    odexpMat.expVal[:,k] = vg_expMat[:,i]
                    classvec.append(0)
                    k+=1
                    sps.append(sp)
                else:
                    spnotfound.append(sp)
            print k
            
        if spclass1:  
            print 'second class'
            for i, sp in enumerate(samples):
                if sp in spclass1:
                    print i,k,sp
                    #spind = samples2.index(sp)
                    odsp.insert(i, sp)
                    odexpMat.expVal[:,k] = vg_expMat[:,i]
                    classvec.append(1)
                    k+=1
                    sps.append(sp)
                else:
                    spnotfound.append(sp)
        
            print k 
        
        
        if spcltest:
            print 'Third class'
            for i, sp in enumerate(samples):
                
                if sp in spcltest:
                    print i,k,sp
                    #spind = samples2.index(sp)
                    odsp.insert(i, sp)
                    odexpMat.expVal[:,k] = vg_expMat[:,i]
                    classvec.append(2)
                    k+=1
                    sps.append(sp)
                else:
                    spnotfound.append(sp)
        
        print k
        
        matfile.write('\t'+",".join(sps).replace(",", "\t")+'\n')
        matfile.write('\t'+",".join(map(str,classvec)).replace(",", "\t")+'\n')
        for i, g in enumerate(genelist):
            el = map( str,list(odexpMat.expVal[i,:]) ) 
            genefile.write(g+'\n')
            matfile.write(g+'\t'+",".join(list( el )).replace(",", "\t") + '\n')




    
    ######### p2p queries   
    def get_p2p_interations(self, genName, interactomelist):
        """
        It queries a db for the interaction partners of gen.
        It adds to the the interactos to the interactomelist.
        """
        self.create_connector()
        cursor=self.conn.cursor()
        query = 'select distinct protA, protB from p2p_db where protA=\"' + genName +'\";'
        
        cursor.execute(query)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        
        interactomelist.append(genName)
        for row in results:
            interactomelist.append( row[1] )
        
        return interactomelist
    
    def get_p2p_interations_byBait(self, genName, interactomeDict):
        """
        It queries a db for the interaction partners of gen.
        It adds to the the interactos to the interactomelist.
        interactomeDict it is a defaultDict
        """
        self.create_connector()
        cursor=self.conn.cursor()
        query = 'select distinct protA, protB from p2p_db where protA=\"' + genName +'\";'
        
        cursor.execute(query)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        
        for row in results:    
            interactomeDict[genName].append( row[1] )
        
        return interactomeDict


    
    
    ####### drugs queries
    
    def get_compound_list(self, tableName, field):
        """
        It queries the the drug GI50 table and returns a
        list of compounds in the database 
        """
        self.create_connector()
        cursor=self.conn.cursor()

        query = 'select distinct '+field+' from ' +tableName+';'
        
        cursor.execute(query)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        
        compoundlist=[]
        for row in results:
            compoundlist.append( int(row[0]) )
        
        return compoundlist


    def get_compsample_val(self, samplelist, complist, gensampleDic,tableName):
        """
        It queries the compound sample database.
        Returns gensampleDic which is a dictionary index by sample and with compound records. 
        """
        
        #ind=0
        notshared=[]
        for j, cmp in enumerate(complist):
            #print str(j)
            cursor=self.conn.cursor()
            query = 'select NSC_ID, PANEL, CELL, NLOGGI50, STDDEV from '+tableName+' where NSC_ID=\"' + str(cmp) +'\";' #erg_signatures
            cursor.execute(query)
            # Fetch all the rows in a list of lists.
            results = cursor.fetchall()
            
            probes,expval,stdval={},{},{}
            for row in results:
                sp = row[2].strip()
                try:
                    probes[sp].append(row[0])
                    expval[sp].append(float(row[3]))
                    stdval[sp].append(float(row[4]))
                except KeyError:
                    probes[sp] = [row[0]]
                    expval[sp] = [float(row[3])]
                    stdval[sp] = [float(row[4])]
            
            #print str(len(set(probes.keys()))), str(len(set(samplelist)))
            
            for sp in samplelist:
                try:
                    GI50 = expval[sp][0] 
                    gistdval = stdval[sp][0]
                    gensampleDic[sp].append( genrecord(probes,cmp, sp, GI50, gistdval) )
                except KeyError:
                    gensampleDic[sp].append( genrecord(probes, cmp, sp, np.nan, np.nan) )
                    #print sp
                    continue
                
        print 'The number of genes not shared between studies = ' + str(len(notshared))
        #print notshared
        
        return gensampleDic


    def get_exp_mat_comp(self,samplelist, complist, tableName):
        """
        returns an array class that contains genelist, samplelist, and 
        a numpy array with the value of the compounds in each sample in samplelist 
        """
        genesampleDic={}
        expmat = cpmMatrix(0, 0)
        nci_samples={}
        for sp in samplelist:
            genesampleDic[sp] = []
            nci_samples[sp]=[]
            # NO: genesampleDic[sp] = genes. Reason: You are pointing everything to the same list
            
        # make a dictionary with the list of genes and samples
        genesampleDic = self.get_compsample_val(samplelist,complist, genesampleDic, tableName)

        j=0
        for sp, spgene in genesampleDic.iteritems():
            print sp, len(spgene)
            if j == 0:
                ngenes = len(spgene)
                nsamples = len(samplelist)
                expmat = cpmMatrix(ngenes, nsamples)
                
            expmat.sample.append((j,sp))
    
            for i, g in enumerate(spgene):
                
                expmat.probeID.append(g.probeID)
                expmat.geneSym.append(str(g.geneSym))
                expmat.geneSynd.add((i,g.geneSym))
                expmat.expValu[i,j] = g.expVal
                expmat.stdVal[i,j] = g.std
                if np.isnan(g.expVal):
                    expmat.expmask[i,j]=True
                else:
                    expmat.expmask[i,j]=False
            j+=1
            
        return expmat


     
    ######### Drug queries to the DrugBanh database
    def get_drugTargetslist(self, genelist,drugDict,tableName):
        
        
        for j, gen in enumerate(genelist):
            #print str(j)
            cursor=self.conn.cursor()
            query = 'select  DB_card, DrugBrand_Name, CAS_Registry_Number, Generic_name, Target_geneName, Target_PDBID, Target_name from '+\
                     tableName+' where Target_geneName=\"' +str(gen) +'\";'
            
            cursor.execute(query)
            # Fetch all the rows in a list of lists.
            results = cursor.fetchall()
            
            for row in results:
                #print row
                dbcard, brandname, casRegistry, genericname, targetGen,targetProt, targetName = \
                                              row[0].strip(),row[1].strip(),row[2].strip(),row[3].strip(),row[4].strip(),row[5].strip(),row[6].strip()
                
                #print dbcard, brandname, casRegistry, genericname, targetGen,targetProt
                drugDict[gen].append(drugBankrecord(dbcard, brandname, casRegistry, genericname, targetGen,targetProt, targetName))
                           
               
        return drugDict


